package com.os.opencv.java.chapter13;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;
import org.opencv.videoio.VideoWriter;

public class VideoWrite {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //创建videocapture对象并打开视屏文件
        VideoCapture vc = new VideoCapture();
        vc.open("pics/2023-11-29_19-50-14.mp4");

        //确认视屏是否打开成功
        if(!vc.isOpened()){
            System.out.println("unable to load video");
            return;
        }
        //初始化视屏写操作
        VideoWriter vw = new VideoWriter();
        Size frameSize = new Size(1280, 720);
        vw.open("out.mp4", VideoWriter.fourcc('D', 'I', 'V', 'X'), 10, frameSize);

        //循环读取视屏
        Mat frame = new Mat();
        Mat canny = new Mat();
        while(true){
            //读取一帧图像并用canny算法提取边缘
            vc.read(frame);
            Imgproc.Canny(frame, canny, 60, 200);

            //将canny边缘图像写入视屏并在屏幕上显示
            vw.write(canny);
            HighGui.imshow("out", canny);
            int index = HighGui.waitKey(100);

            //如按esc键退出
            if(index == 27){
                //释放videocapture和videowriter占用的资源
                vc.release();
                vw.release();
                break;
            }
        }

        HighGui.waitKey(0);

    }
}
